/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-7-26
 * Time: 上午1:53
 * To change this template use File | Settings | File Templates.
 */
var Register = function () {

    var handleRegister = function () {

        function format(state) {
            if (!state.id) return state.text; // optgroup
            return "<img class='flag' src='assets/img/flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
        }

        $("#select2_sample4").select2({
            placeholder: '<i class="fa fa-map-marker"></i>&nbsp;请选择国家',
            allowClear: true,
            formatResult: format,
            formatSelection: format,
            escapeMarkup: function (m) {
                return m;
            }
        });

        $('#select2_sample4').change(function () {
            $('.register-form').validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
        });


        $('.register-form').validate({
            errorElement: 'span', // default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {
                phone: {
                    required: false
                },
                email: {
                    required: false,
                    email: true
                },
                address: {
                    required: false
                },
                country: {
                    required: false
                },
                loginName: {
                    required: true,
                    remote: {
                        url: "public/checkRepeatLoginName",
                        type: "post",
                        data: {
                            loginName: function () {
                                return $("#loginName").val();
                            },
                            forgetPassword: function () {
                                return false;
                            },
                            //必须要给他们定义方法；不能直接传至那样就是个死数；到时间触发不会发生变化；定义方法就会走方法重新获取走逻辑
                            oldName: function () {
                                return "";
                            }
                        }
                    }
                },
                userName: {
                    required: true
                },
                question: {
                    required: true
                },
                password: {
                    required: true,
                    minlength: 3,
                    maxlength: 18
                },
                rpassword: {
                    equalTo: "#register_password"
                },
                tnc: {
                    required: true
                }
            },

            messages: { // custom messages for radio buttons and checkboxes
                tnc: {
                    required: "请您先接受 TNC 协议！"
                },
                userName: {
                    required: "姓名不能为空！"
                },
                question: {
                    required: "确认信息不能为空！以便找回您的密码"
                },
                rpassword: {
                    equalTo: "密码不一致！"
                },
                email:{
                    email:"邮箱格式不正确，请检查好输入！"
                },
                password: {
                    required: "密码不能为空！",
                    minlength: "密码长度不能小于3位",
                    maxlength: "密码长度不能超出18位"
                },
                loginName: {
                    required: "账号不能为空！",
                    remote: "账号已存在!"
                }
            },
            invalidHandler: function (event, validator) { //display error alert on form submit

            },

            highlight: function (element) { // hightlight error inputs
                $(element).closest('.form-group').addClass('has-error');
                // set error class to the control group
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                if (element.attr("name") == "tnc") { // insert checkbox errors after the container
                    error.insertAfter($('#register_tnc_error'));
                } else if (element.closest('.input-icon').size() === 1) {
                    error.insertAfter(element.closest('.input-icon'));
                } else {
                    error.insertAfter(element);
                }
            },

            submitHandler: function (form) {
                form.submit();
            }
        });
        $("#loginName").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#userName").focus();
            }
        });
        $("#userName").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#register_password").focus();
            }
        });
        $("#register_password").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#rpassword").focus();
            }
        });
        $("#rpassword").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#question").focus();
            }
        });
        $("#question").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#phone").focus();
            }
        });
        $("#phone").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#registerEmail").focus();
            }
        });
        $("#registerEmail").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#address").focus();
            }
        });
        $("#address").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#select2_sample4").focus();
            }
        });

        $('.register-form input').keypress(function (e) {
            if (e.which == 13) {
                var register_form = $('.register-form');
                if (register_form.validate().form()) {
                    register_form.submit();
                }
                return false;
            }
        });

        jQuery('#register-back-btn').click(function () {
            history.go(-1);
        });

    };

    return {
        //main function to initiate the module
        init: function () {

            handleRegister();

            $.backstretch([
                "img/login/bg/02.jpg",
//                "img/login/bg/01.jpg",
                "img/login/bg/04.jpg",
                "img/login/bg/05.jpg"
            ], {
                fade: 1000,
                duration: 5000
            });
        }

    };

}();